﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace BL
{
	public class Table
	{
		static Table instance;
		public static Table Instance
		{
			get { return instance; }
		}

		List<TableRow> table = new List<TableRow>();
		TableItem[,] items = new TableItem[16,6];
		long maxID = 0;

		public Table(List<Entity> ti)
		{
			instance = this;
			for (int i=0; i<16; i++)
				table.Add(new TableRow());
			foreach (Entity e in ti)
			{
				TableItem item = (TableItem)e;
				int index = (int)item.Pair * 2 + (int)item.Week;
				table[index].Cells[(int)item.Day] = item.Item.ToString();
                item.Item = Subjects.Instance[item.Item.ID];
                items[index, (int)item.Day] = item;
				if (item.ID > maxID)
					maxID = item.ID;
			}
		}

        public bool HasSubjectItem(SubjectItem si)
        {
            for (int i = 0; i < 16; i++)
            {
                for (int j = 0; j < 6; j++)
                {
                    if ((items[i, j] != null) && (items[i, j].Item.ID == si.ID))
                            return true;
                }
            }
            return false;
        }

        public List<int> GetEmptyPair(Weeks week, Days day)
        {
            List<int> emptyPairs = new List<int>();
            for (int i = (int)week; i < 16; i += 2)
            {
                if (items[i, (int)day] == null)
                    emptyPairs.Add(i / 20);
            }
            return emptyPairs;
        }

        public TableRow[] Rows
		{
			get 
            {
                int j=0;
                foreach (TableRow row in table)
                {
                    for (int i = 0; i < 6; i++)
                    {
                        if (items[j,i] != null && items[j,i].Item != null)
                            row.Cells[i] = items[j, i].Item.ToString();
                    }
                    j++;
                }
                return table.ToArray(); 
            }
		}

		public TableItem ChangeCell(int row, int col, string newValue)
		{
			if (items[row, col] == null && newValue != "")
			{// Создаем ячейку расписания
				TableItem ti = new TableItem();
				ti.ID = ++maxID;
				ti.Day = (Days)col;
				ti.Week = (Weeks)(row % 2);
				ti.Pair = row / 2;
				ti.Item = Subjects.Instance[newValue];
				items[row, col] = ti;
				table[row].Cells[col] = newValue;
				return ti;
			}
			if (items[row, col] != null && (newValue == "" || 
                Subjects.Instance[newValue].LessonType == LessonTypes.Курсовое ||
                Subjects.Instance[newValue].LessonType == LessonTypes.Дипломное ||
                Subjects.Instance[newValue].LessonType == LessonTypes.Экзамен ||
                Subjects.Instance[newValue].LessonType == LessonTypes.Консультация))
			{// Очищаем ячейку расписания
				TableItem ti = items[row, col];
				items[row, col] = null;
				table[row].Cells[col] = "";
				return ti;
			}
			if (items[row, col] != null && newValue != "")
			{// Обновляем ячейку
				TableItem ti = items[row, col];
				ti.Item = Subjects.Instance[newValue];
				table[row].Cells[col] = newValue;
				return ti;
			}
			return null;
		}

        public void Delete(long subjectID)
        {
            for (int i = 0; i < table.Count; i++)
            {
                string[] cells = table[i].Cells;
                for (int j=0; j<cells.Length; j++)
                {
                    if (cells[j] == Subjects.Instance[subjectID].ToString())
                        cells[j] = "";
                }
            }
            for (int i=0; i<items.GetLength(0); i++)
            {
                for (int j=0; j<items.GetLength(1); j++)
                {
                    if (items[i,j] != null && items[i,j].Item.ID == subjectID)
                        items[i,j] = null;
                }
            }
        }

        public List<TableItem> GetItemsAtDay(Days day, Weeks week)
        {
            List<TableItem> items = new List<TableItem>();
            for (int i = 0; i < 16; i++)
            {
                TableItem ti = this.items[i, (int)day];
                if (ti != null && ti.Week == week)
                    items.Add(ti);
            }
            return items;
        }
    }
}
